package com.net.shop.car.manager.utils.payment;

import java.net.URLEncoder;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;

import com.alipay.android.app.sdk.AliPay;
import com.net.shop.car.manager.App;
import com.net.shop.car.manager.Constant;

public class PaymentUtils {
	private static final int RQF_PAY = 1;

	private static final int RQF_LOGIN = 2;

	private PaymentUtils() {
	}

	private static PaymentUtils instance = new PaymentUtils();

	private Context context;
	private Product product;

	public static PaymentUtils getInstance() {
		return instance;
	}

	public void setContext(Context context) {
		this.context = context;
	}

	// public static class Product {
	// public String order;
	// // 商品名称
	// public String subject;
	// // 商品详情
	// public String body;
	// public String price;
	// }

	public Product getProduct() {
		return product;
	}

	public void setProduct(Product product) {
		this.product = product;
	}

	private String getNewOrderInfo(Product product) {
		StringBuilder sb = new StringBuilder();
		sb.append("partner=\"");
		sb.append(Keys.DEFAULT_PARTNER);
		sb.append("\"&out_trade_no=\"");
		sb.append(product.getOrder());
		sb.append("\"&subject=\"");
		sb.append(product.getSubject());
		sb.append("\"&body=\"");
		sb.append(product.getBody());
		sb.append("\"&total_fee=\"");
		sb.append(product.getPrice());

		sb.append("\"&notify_url=\"");
		// 网址�?要做URL编码
		sb.append(URLEncoder.encode(Constant.payment.notify_url));
		sb.append("\"&service=\"mobile.securitypay.pay");
		sb.append("\"&_input_charset=\"UTF-8");
		sb.append("\"&return_url=\"");
		sb.append(URLEncoder.encode("http://m.alipay.com"));
		sb.append("\"&payment_type=\"1");
		sb.append("\"&seller_id=\"");
		sb.append(Keys.DEFAULT_SELLER);

		// 如果show_url值为空，可不�?
		// sb.append("\"&show_url=\"");
		sb.append("\"&it_b_pay=\"1m");
		sb.append("\"");

		return new String(sb);
	}

	private String getSignType() {
		return "sign_type=\"RSA\"";
	}

	public void pay(Product product) {
		String info = getNewOrderInfo(product);
		String sign = Rsa.sign(info, Keys.PRIVATE);
		sign = URLEncoder.encode(sign);
		info += "&sign=\"" + sign + "\"&" + getSignType();
		final String orderInfo = info;
		System.out.println(orderInfo);
		new Thread() {
			public void run() {

				// 获取Alipay对象，构造参数为当前Activity和Handler实例对象
				AliPay alipay = new AliPay((Activity) context, mHandler);

				// 调用pay方法，将订单信息传入
				String result = alipay.pay(orderInfo);
				// 处理返回结果
				Message msg = new Message();
				msg.what = RQF_PAY;
				msg.obj = result;
				mHandler.sendMessage(msg);
				// Toast.makeText(RegistrationActivity.this, result,
				// Toast.LENGTH_SHORT).show();
			}
		}.start();
	}

	Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			// Result result = new Result((String) msg.obj);
			switch (msg.what) {
			case RQF_PAY:
			case RQF_LOGIN: {
				String resultStr = msg.obj.toString();
				if (resultStr.contains("success=\"true\"")) {
					Toast.makeText(context, "支付成功", Toast.LENGTH_SHORT).show();
				} else {
					Toast.makeText(context, "支付失败", Toast.LENGTH_SHORT).show();
				}
			}
				break;
			default:
				break;
			}
		};
	};

}
